Skip to content

Method: static {...}

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.oom;
19:
20: import cz.cvut.kbss.jopa.model.MultilingualString;
21: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
22: import cz.cvut.kbss.jopa.model.metamodel.AbstractAttribute;
23: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
24: import cz.cvut.kbss.jopa.oom.converter.ToLexicalFormConverter;
25: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
26: import cz.cvut.kbss.ontodriver.model.Assertion;
27: import cz.cvut.kbss.ontodriver.model.Axiom;
28: import cz.cvut.kbss.ontodriver.model.NamedResource;
29: import cz.cvut.kbss.ontodriver.model.Value;
30:
31: import java.net.URI;
32: import java.net.URL;
33: import java.util.stream.Collectors;
34:
35: class SingularAnnotationPropertyStrategy<X> extends SingularDataPropertyStrategy<X> {
36:
37: SingularAnnotationPropertyStrategy(EntityType<X> et, AbstractAttribute<? super X, ?> att, Descriptor descriptor,
38: EntityMappingHelper mapper) {
39: super(et, att, descriptor, mapper);
40: }
41:
42: @Override
43: void addAxiomValue(Axiom<?> ax) {
44: final Object val = ax.getValue().getValue();
45: if (!isValidRange(val)) {
46: return;
47: }
48: verifyCardinalityConstraint(ax.getSubject());
49: if (IdentifierTransformer.isValidIdentifierType(attribute.getJavaType())) {
50: this.value = IdentifierTransformer
51: .transformToIdentifier(ToLexicalFormConverter.INSTANCE.convertToAttribute(val),
52: attribute.getJavaType());
53: } else {
54: this.value = toAttributeValue(val);
55: }
56: }
57:
58: @Override
59: boolean isValidRange(Object value) {
60: assert value != null;
61:
62: return value instanceof NamedResource && IdentifierTransformer.isValidIdentifierType(attribute.getJavaType()) ||
63: super.isValidRange(value);
64: }
65:
66: @Override
67: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) {
68: final Object value = extractFieldValueFromInstance(instance);
69: if (value == null) {
70: valueBuilder.addValue(createAssertion(), Value.nullValue(), getAttributeWriteContext());
71: return;
72: }
73:
74: if (isResourceIdentifierType(attribute.getJavaType())) {
75: valueBuilder.addValue(createAssertion(),
76: new Value<>(NamedResource.create(IdentifierTransformer.valueAsUri(value))),
77: getAttributeWriteContext());
78: } else if (value instanceof MultilingualString) {
79: valueBuilder.addValues(createAssertion(),
80: SingularMultilingualStringFieldStrategy.translationsToLangStrings(
81: (MultilingualString) value)
82: .collect(Collectors.toList()),
83: getAttributeWriteContext());
84: } else {
85: valueBuilder.addValue(createAssertion(), convertToAxiomValue(value), getAttributeWriteContext());
86: }
87: }
88:
89: @Override
90: Assertion createAssertion() {
91: return Assertion
92: .createAnnotationPropertyAssertion(attribute.getIRI().toURI(), getLanguage(), attribute.isInferred());
93: }
94:
95: /**
96: * Checks whether the specified class can be used to represent a resource identifier in an annotation property
97: * value.
98: * <p>
99: * This means that only instances of types for which this method returns {@code true} are treated as resource
100: * identifiers when mapping to the underlying repository.
101: *
102: * @param cls Type to check
103: * @return {@code true} if the specified type represents resource identifier w.r.t. annotation properties, {@code
104: * false} otherwise
105: */
106: static boolean isResourceIdentifierType(Class<?> cls) {
107: return URI.class.equals(cls) || URL.class.equals(cls);
108: }
109: }